﻿using System.Collections.Generic;
using System.Linq;
using Best.SmartCms.Config;
using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Domain.Common;
using Best.SmartCms.Dto.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service.Common
{
    /// <summary>
    /// 文件服务
    /// </summary>
    public class FileService:IService
    {

        private readonly AttachmentDao _attachmentDao;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
        public FileService(MySqlDbContext mySqlDbContext)
        {
            _attachmentDao = new AttachmentDao(mySqlDbContext);
        }

        /// <summary>
        /// 获取上次文件
        /// </summary>
        /// <param name="sourceid"></param>
        /// <param name="sourceType"></param>
        /// <returns></returns>
        public List<UploadFileDto> GetFiles(long sourceid, int sourceType)
        {
            var list = _attachmentDao.FindBySourceId(sourceid, sourceType);
            return list.Select(p => new UploadFileDto
            {
                Id = p.Id,
                Uid = p.Id.ToString(),
                Size = p.Size,
                Type = p.Type,
                Name = p.Name,
                Status = "done",
                OssKey = p.OssKey,
                Url = p.Url,
                ThumbUrl = p.ThumbUrl
            }).ToList();
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="files"></param>
        /// <param name="sourceid"></param>
        /// <param name="sourceType"></param>
        public void SaveFiles(List<UploadFileDto> files,long sourceid, int sourceType)
        {
            var list = _attachmentDao.FindBySourceId(sourceid, sourceType);
            var oldids = list.Select(p => p.Id.ToString()).ToList();
            var newids = files.Select(p => p.Uid).ToList();
            var addfiles = files.Where(p => !oldids.Contains(p.Uid.ToString()));
            var delfiles = list.Where(p => !newids.Contains(p.Id.ToString()));
            foreach (var p in delfiles)
            {
                _attachmentDao.Delete(p);
            }

            foreach (var p in addfiles)
            {
                _attachmentDao.Add(new Attachment{
                    Size = p.Size,
                    Type = p.Type,
                    Name = p.Name,
                    OssKey = p.OssKey,
                    Url = p.Url,
                    ThumbUrl = p.ThumbUrl,
                    SourceType = sourceType,
                    SourceId = sourceid
                });
            }
        }

        /// <summary>
        /// 字节流上传图片
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="objectKey"></param>
        /// <param name="fileLenth"></param>
        /// <param name="fileName"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public UploadFileDto UploadImageByByte(byte[] bytes, string objectKey,long fileLenth,string fileName,string type)
        {
            var result = new UploadFileDto();
            //缩略图url
            var url = string.Format(Configs.OssPicUrl,objectKey);
            //原始图片url
            var oraginalUrl = string.Format(Configs.OriginalPicUrl, objectKey);
            var upLoadResult = new OssService().SaveFileToOssByBytes(bytes, objectKey);
            if (upLoadResult)
            {
                result.Url = oraginalUrl;
                result.OssKey = objectKey;
                result.Size = fileLenth;
                result.Name = fileName;
                result.Type = type;
                result.ThumbUrl = url;
            }
            return result;
        }

        /// <summary>
        /// 从Oss获取文件流
        /// </summary>
        /// <param name="objectKey"></param>
        public byte[] GetOssImageByte(string objectKey)
        {
            return new OssService().GetFileFromOss(objectKey);
        }
    }
}
